package com.david.service;

import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 用户登录查询
 */
@Service
public class MyDetailsService implements UserDetailsService {

	
//	@Autowired
//	UserDao userDao;


	/**
	 * 方法作用：查询对应的用户
	 *
	 * 完全自定义登录时候的查询User的行为（使用age、sex、phonenumber查询都可以），脱离了自动配置的数据源，使用redis、mysql或者是jpa都可以
	 */
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

		//伪代码:查询数据库中的User

		if(new Random().nextBoolean()) {

			//假设登录成功
			throw new CredentialsExpiredException("密码已过期，请修改密码之后 在继续操作");
			
		}else {
			throw new LockedException("用户已经被锁定，请联系管理员");
		}
	}
	
	

}
